/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package basedatos;

import java.sql.Statement;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Collection;
import logica.cRecambio;

/**
 *
 * @author Alberto
 */
public class GestorRecambios {

    private static GestorRecambios INSTANCE = null;
    private String user;
    private String password;
    private String url;

    private GestorRecambios() {
        this.user = "root";
        this.password = "root";
        this.url = "jdbc:mysql://localhost:3306/repuestos_bd";
    }

    public static GestorRecambios getInstance() {
        if (INSTANCE == null) {
            INSTANCE = new GestorRecambios();
        }
        return INSTANCE;
    }

    static {
        try {
            Class.forName("com.mysql.jdbc.Driver");
        } catch (Exception e) {
            System.out.println("Error en la conexion");
        }
    }

    private Connection getConnection() throws SQLException {
        return DriverManager.getConnection(url, user, password);
    }

    public void insertarRecambio(cRecambio c) {
        String query = "INSERT INTO recambios (cod_recambio, unidades, nombre_recambio, descripcion)"
                + "VALUES ('" + c.getCodRecambio() + "', '" + c.getUnidades() + "', '" + c.getNombreRecambio() + "', '" + c.getDescripcion() + "')";
//        System.out.println("insertarRecambio() query=" + query);
        Connection con = null;
        Statement pStmt = null;
        try {
            con = getConnection();
            pStmt = con.createStatement();
            pStmt.execute(query);

        } catch (Exception e) {
            /* CAPTURAR EXCEPTION */
        } finally {
            if (pStmt != null) {
                try {
                    pStmt.close();
                } catch (SQLException e) {
                }
            }
            if (con != null) {
                try {
                    con.close();
                } catch (SQLException e) {
                }
            }
        }
    }

    public void borrarRecmabio(cRecambio c) throws SQLException {
        String query = "DELETE FROM recambios WHERE cod_recambio= '" + c.getCodRecambio() + "'";
        Connection con = null;
        Statement pStmt = null;
        try {
            con = getConnection();
            pStmt = con.createStatement();
            pStmt.execute(query);

        } catch (Exception e) {
            /* CAPTURAR EXCEPTION */
        } finally {
            if (pStmt != null) {
                try {
                    pStmt.close();
                } catch (SQLException e) {
                }
            }
            if (con != null) {
                try {
                    con.close();
                } catch (SQLException e) {
                }
            }
        }
    }

    public void actulaizarRecambio(cRecambio c) throws SQLException {
        String query = "UPDATE recambios SET unidades= '" + c.getUnidades() + "', nombre_recambio= '" + c.getNombreRecambio() + "', descripcion= '" + c.getDescripcion()
                + "' where cod_recambio= '" + c.getCodRecambio() + "'";
        Connection con = null;
        Statement pStmt = null;
        try {
            con = getConnection();
            pStmt = con.createStatement();
            pStmt.executeUpdate(query);

        } catch (Exception e) {
            /* CAPTURAR EXCEPTION */
        } finally {
            if (pStmt != null) {
                try {
                    pStmt.close();
                } catch (SQLException e) {
                }
            }
            if (con != null) {
                try {
                    con.close();
                } catch (SQLException e) {
                }
            }
        }
    }

    public Collection<cRecambio> getRecambio() {
        Collection<cRecambio> recambios = new ArrayList<cRecambio>();
//        System.out.println("Recambios "+recambios.size());
        String query = "SELECT cod_recambio, unidades, nombre_recambio, descripcion from recambios order by cod_recambio";
//        System.out.println(" QUERY: "+query);
        Connection con = null;
        Statement pStmt = null;
        try {
            con = getConnection();
            pStmt = con.createStatement();
            ResultSet set = pStmt.executeQuery(query);
            while (set.next()) {
                cRecambio c = new cRecambio();
                c.setCodRecambio(set.getInt("cod_recambio"));
                c.setUnidades(set.getInt("unidades"));
                c.setNombreRecambio(set.getString("nombre_recambio"));
                c.setDescripcion(set.getString("descripcion"));
                recambios.add(c);
            }
        } catch (Exception e) {
            System.out.println("ERROR: " + e.getMessage());
            /* CAPTURAR EXCEPTION */
        } finally {
            if (pStmt != null) {
                try {
                    pStmt.close();
                } catch (SQLException e) {
                }
            }
            if (con != null) {
                try {
                    con.close();
                } catch (SQLException e) {
                }
            }
            return recambios;
        }
    }

    public boolean existeRecambio(int codRecambio) {
        String query = "SELECT * from recambios where cod_recambio = '" + codRecambio + "'";
        Connection con = null;
        Statement pStmt = null;
        try {
            con = getConnection();
            pStmt = con.createStatement();
            ResultSet set = pStmt.executeQuery(query);
            if (set.next()) {
                return true;
            }
            return false;
        } catch (Exception e) {
            return false;
        } finally {
            if (pStmt != null) {
                try {
                    pStmt.close();
                } catch (SQLException e) {
                }
            }
            if (con != null) {
                try {
                    con.close();
                } catch (SQLException e) {
                }
            }
        }
    }
}